package com.rongshu.view.discovery.adapter;

import android.animation.Animator;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.rongshu.R;
import com.rongshu.utils.AnimationUtil;
import com.rongshu.utils.LoginUtils;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.discovery.bean.AuthorDetailBean;
import com.rongshu.view.home.listener.OnItemMoreClickListener;
import com.utils.FastClickUtils;
import com.widget.CircleImageView;
import com.widget.loading.AnimatorListener;

import java.util.List;


public class TaAttentionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<AuthorDetailBean> mAuthorDetailBeans = null;
    private Context mContext = null;
    //item
    private OnItemMoreClickListener mOnItemClickListener = null;

    public TaAttentionAdapter(Context context, List<AuthorDetailBean> mAuthorDetailBeans) {
        this.mContext = context;
        this.mAuthorDetailBeans = mAuthorDetailBeans;
    }

    public List<AuthorDetailBean> getAuthorDetailBeans() {
        return this.mAuthorDetailBeans;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_ta_attention_item, parent, false);
        return new BlackListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final BlackListViewHolder blackListViewHolder = (BlackListViewHolder) holder;
        final AuthorDetailBean authorDetailBean = mAuthorDetailBeans.get(position);
        //头像
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.user_head_icon);
        Glide.with(mContext)
                .load(authorDetailBean.getAvatar())
                .apply(options)
                .into(blackListViewHolder.blacklist_circle_imageview);
        //vip
        blackListViewHolder.iv_vip.setVisibility(authorDetailBean.getVip() ? View.VISIBLE : View.GONE);
        //bi_name
        blackListViewHolder.blacklist_user_name.setText(authorDetailBean.getBi_name());
        //状态
        if (authorDetailBean.getIs_guanzhu() != 0 && authorDetailBean.getTais_guanzhu() != 0) {
            blackListViewHolder.keepUser.setText(mContext.getString(R.string.mutual_attention));
            blackListViewHolder.keepUser.setTextColor(ContextCompat.getColor(mContext, R.color.color_9f9f9f));
            blackListViewHolder.keepUserImg.setVisibility(View.GONE);
        } else if (authorDetailBean.getIs_guanzhu() != 0) {
            blackListViewHolder.keepUser.setText(mContext.getString(R.string.already_attention));
            blackListViewHolder.keepUser.setTextColor(ContextCompat.getColor(mContext, R.color.color_9f9f9f));
            blackListViewHolder.keepUserImg.setVisibility(View.GONE);
        } else {
            blackListViewHolder.keepUser.setText("关注");
            blackListViewHolder.keepUser.setTextColor(ContextCompat.getColor(mContext, R.color.color_3cc051));
            blackListViewHolder.keepUserImg.setVisibility(View.VISIBLE);
        }
        blackListViewHolder.addAttentionLayout.setVisibility(authorDetailBean.getUser_id_to() == UserInfoUtils.getUserId() ? View.GONE : View.VISIBLE);


        //item click
        blackListViewHolder.addAttentionLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (mOnItemClickListener != null && !FastClickUtils.isFastClick()) {
                    if (UserInfoUtils.getUserInfo() != null){
                        if (authorDetailBean.getIs_guanzhu() == 1) {
                            AnimationUtil.AlphaScaleShrinkAnimator(v, new AnimatorListener() {
                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    v.animate().alpha(1).scaleX(1).scaleY(1);
                                }
                            });
                        } else {
                            AnimationUtil.AlphaScaleAnimator(v, new AnimatorListener() {
                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    v.animate().alpha(1).scaleX(1).scaleY(1);
                                }
                            });
                        }
                        mOnItemClickListener.onItemClick(blackListViewHolder.addAttentionLayout, position);
                    } else {
                        LoginUtils.toLogin(mContext);
                    }
                }
            }
        });
        //header click
        blackListViewHolder.blacklist_circle_imageview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onUserHeaderClick(position, authorDetailBean.getUser_id_to(), (authorDetailBean.getIs_allow_browse_homepage() == 1));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mAuthorDetailBeans == null) {
            return 0;
        }
        return mAuthorDetailBeans.size();
    }

    public static class BlackListViewHolder extends RecyclerView.ViewHolder {

        private CircleImageView blacklist_circle_imageview;
        private TextView blacklist_user_name, keepUser;
        private ImageView iv_vip, keepUserImg;
        private LinearLayout addAttentionLayout;

        public BlackListViewHolder(View itemView) {
            super(itemView);
            blacklist_circle_imageview = itemView.findViewById(R.id.blacklist_circle_imageview);
            blacklist_user_name = itemView.findViewById(R.id.blacklist_user_name);
            iv_vip = itemView.findViewById(R.id.iv_vip);
            addAttentionLayout = itemView.findViewById(R.id.layout_add_attention);
            keepUser = itemView.findViewById(R.id.keep_user);
            keepUserImg = itemView.findViewById(R.id.keep_author_icon);
        }
    }

    /**
     * set item litener
     *
     * @param listener
     */
    public void setOnItemChildClickListener(OnItemMoreClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
